package com.sgb.kjwl.view.ui.activity.self.goods;


import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.core.util.Pair;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMemberPriceDetailsBinding;
import com.sgb.kjwl.model.entity.MemberPriceEntity;
import com.sgb.kjwl.view.ui.adapter.self.goods.MemberPriceDetailsAdapter;
import com.sgb.kjwl.view.widget.self.goods.ShowVipPriceDetailWindow;
import com.sgb.kjwl.viewmodel.self.goods.AddMemberGoodsModel;
import com.sgb.link.router.PathGoods;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DateUtils;


import java.io.Serializable;
import java.util.List;
import java.util.LinkedHashMap;


/**
 * 作者:陈方凯
 * 日期:2021/11/16
 * 说明:会员价详情
 */
@Route(path = PathGoods.MemberPriceDetailsActivity)
public class MemberPriceDetailsActivity extends BaseActivity<AddMemberGoodsModel, ActivityMemberPriceDetailsBinding> {
    private MemberPriceDetailsAdapter mAdapter;
    private ShowVipPriceDetailWindow mPopWindow;

    private String itemMemberId = "";
    private String itemId = "";

    @Override
    protected int layoutId() {
        return R.layout.activity_member_price_details;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(AddMemberGoodsModel.class);
        mBinding.setModel(mViewModel);
    }

    @Override
    protected void init() {
        itemMemberId = getIntent().getStringExtra("itemMemberId");
        itemId = getIntent().getStringExtra("itemId");

        mBinding.backView.setOnClickListener(this);
        mBinding.tvLookDetail.setOnClickListener(this);
        mBinding.imgLookDetail.setOnClickListener(this);
        initView();
        initObserver();
        mViewModel.getMemberPriceDetails(itemMemberId, itemId);
    }

    private void initView() {
        mAdapter = new MemberPriceDetailsAdapter(this, null);
        mAdapter.setItemSelect(false);
        mBinding.rvPriceList.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvPriceList.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<MemberPriceEntity.MemberPriceSkuDetail>() {
            @Override
            public void onItemClick(MemberPriceEntity.MemberPriceSkuDetail memberPriceSkuDetail, int position) {
                mViewModel.getSkuMemberPriceDetail(memberPriceSkuDetail.getItemMemberNo(), memberPriceSkuDetail.getSkuId());

            }
        });
    }

    private void initObserver() {
        mViewModel.detail.observe(this, new Observer<MemberPriceEntity>() {
            @Override
            public void onChanged(MemberPriceEntity memberPriceEntity) {
                if (memberPriceEntity.getMemberPriceSkuDetail() != null)
                    mAdapter.setDatas(memberPriceEntity.getMemberPriceSkuDetail());

                if (memberPriceEntity.getItemMember().getState() == 0) {
                    mBinding.tvStatus.setText("已失效");
                    mBinding.tvStatus.setBackground(getResources().getDrawable(R.drawable.corner86_c0c4cc_left_press_bg));
                    mBinding.tvStatus.setTextColor(getResources().getColor(R.color.cl_c0c4cc));
                } else if (memberPriceEntity.getItemMember().getState() == 1) {
                    mBinding.tvStatus.setText("生效中");
                    mBinding.tvStatus.setBackground(getResources().getDrawable(R.drawable.corner86_5792fd_left_press_bg));
                    mBinding.tvStatus.setTextColor(getResources().getColor(R.color.new_blue));
                }
                mBinding.tvUpdateTime.setText(DateUtils.timestampTo13Date3(Long.valueOf(memberPriceEntity.getItemMember().getCreateTime())));
            }
        });
        mViewModel.detailSku.observe(this, new Observer<List<MemberPriceEntity.MemberPriceSkuDetail>>() {
            @Override
            public void onChanged(List<MemberPriceEntity.MemberPriceSkuDetail> memberPriceSkuDetail) {
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                map.put("规格属性", memberPriceSkuDetail.get(0).getSkuDetails());
                if (memberPriceSkuDetail.get(0).getParityPrice() != null && (!TextUtils.isEmpty(memberPriceSkuDetail.get(0).getPromotionPrice())) && !memberPriceSkuDetail.get(0).getPromotionPrice().equals("0.00"))
                    map.put("销售价/促销价", "¥" + memberPriceSkuDetail.get(0).getParityPrice() + "/¥" + memberPriceSkuDetail.get(0).getPromotionPrice());
                if (memberPriceSkuDetail.get(0).getParityPrice() != null && (TextUtils.isEmpty(memberPriceSkuDetail.get(0).getPromotionPrice()) || memberPriceSkuDetail.get(0).getPromotionPrice().equals("0.00")))
                    map.put("销售价", "¥" + memberPriceSkuDetail.get(0).getParityPrice());
                if (memberPriceSkuDetail.get(0).getParityPrice() == null && memberPriceSkuDetail.get(0).getPromotionPrice() != null)
                    map.put("促销价", "¥" + memberPriceSkuDetail.get(0).getPromotionPrice());
                for (int i = 0; i < memberPriceSkuDetail.size(); i++) {
                    switch (memberPriceSkuDetail.get(i).getLevel()) {
                        case 0:
                            map.put("普通会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                            break;
                        case 1:
                            map.put("一星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                            break;
                        case 2:
                            map.put("二星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                            break;
                        case 3:
                            map.put("三星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                            break;
                        case 4:
                            map.put("四星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                            break;
                        case 5:
                            map.put("五星会员", "¥" + memberPriceSkuDetail.get(i).getPrice());
                            break;
                    }
                }
                showPopWindow(map);
            }
        });
    }


    private void showPopWindow(LinkedHashMap<String, String> map) {
        if (mPopWindow == null) {
            mPopWindow = new ShowVipPriceDetailWindow(this);
        }
        mPopWindow.setBgView(mBinding.grayLayout);
        mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        mPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                mPopWindow.dismiss();
            }
        });
        mPopWindow.updateData(map, "会员价详情", "", "关闭");
        mPopWindow.showDownPopwindow(mBinding.grayLayout, true);
    }


    @Override
    protected void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.back_view) {
            finish();
        } else if (id == R.id.tv_look_detail || id == R.id.img_look_detail) {
            if (mViewModel.detail.getValue() != null) {
                ActivityJump.startActivity(PathGoods.OperationRecordActivity, Pair.create("MemberPriceEntity", (Serializable) (mViewModel.detail.getValue())));
            } else {
                mViewModel.mToast.postValue("详情数据异常");
            }
        }
    }
}
